package com.meihuayishu.backend.config;

import com.meihuayishu.backend.entity.Hexagram;
import com.meihuayishu.backend.entity.YaoCi;
import com.meihuayishu.backend.repository.HexagramRepository;
import com.meihuayishu.backend.repository.YaoCiRepository;
import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;

@Component
@RequiredArgsConstructor
@Slf4j
public class DataInitializer {
    
    private final HexagramRepository hexagramRepository;
    private final YaoCiRepository yaoCiRepository;
    
    @PostConstruct
    public void init() {
        // 检查是否已经初始化过数据
        if (hexagramRepository.count() > 0) {
            log.info("卦象数据已存在，跳过初始化");
            return;
        }
        
        log.info("开始初始化64卦数据");
        initHexagrams();
        log.info("64卦数据初始化完成");
    }
    
    private void initHexagrams() {
        // 创建64卦的基本信息
        List<Hexagram> hexagrams = Arrays.asList(
            createHexagram(1L, "乾卦", "111111", "元亨利贞", "至哉坤元，万物资生", "天行健，君子以自强不息", "金", "纯阳之卦，刚健不息", "天", "父亲", "刚健、进取", "秋冬", "西北方"),
            createHexagram(2L, "坤卦", "000000", "元亨，利牝马之贞", "至哉坤元，万物资生", "地势坤，君子以厚德载物", "土", "纯阴之卦，柔顺包容", "地", "母亲", "柔顺、包容", "春夏", "西南方"),
            createHexagram(3L, "屯卦", "100010", "元亨利贞，勿用有攸往，利建侯", "屯，刚柔始交而难生", "云雷屯，君子以经纶", "土", "初生之卦，艰难创始", "雷水", "长男", "初生、艰难", "春", "东方"),
            createHexagram(4L, "蒙卦", "010001", "亨，匪我求童蒙，童蒙求我", "蒙，山下有险", "山下出泉，君子以果行育德", "水", "启蒙之卦，教育成长", "山水", "少男", "蒙昧、教育", "春", "东北方"),
            createHexagram(5L, "需卦", "111010", "有孚光亨，贞吉，利涉大川", "需，须也，险在前也", "云上于天，君子以饮食宴乐", "水", "等待之卦，蓄势待发", "水天", "长男", "等待、需求", "冬", "西方"),
            createHexagram(6L, "讼卦", "010111", "有孚窒惕，中吉，终凶", "讼，上刚下险", "天与水违行，君子以作事谋始", "金", "争讼之卦，争议冲突", "天水", "长男", "争讼、争议", "秋", "西南方"),
            createHexagram(7L, "师卦", "010000", "贞丈人吉，无咎", "师，众也", "地中有水，君子以容民畜众", "土", "军队之卦，组织管理", "地水", "长男", "军队、组织", "夏", "西南方"),
            createHexagram(8L, "比卦", "000010", "吉，原筮元永贞，无咎", "比，吉也", "地上有水，先王以建万国亲诸侯", "水", "亲比之卦，团结合作", "水地", "长男", "亲比、团结", "夏", "西南方"),
            createHexagram(9L, "小畜卦", "110111", "亨，密云不雨", "小畜，柔得位而上下应之", "风行天上，君子以懿文德", "金", "小有积蓄，循序渐进", "风天", "长女", "小畜、积累", "春夏之交", "西南方"),
            createHexagram(10L, "履卦", "111001", "履虎尾，不咥人，亨", "履，柔履刚也", "上天下泽，君子以辨上下定民志", "金", "践行之卦，谨慎行事", "天泽", "长男", "履行、谨慎", "秋冬之交", "西南方"),
            createHexagram(11L, "泰卦", "111000", "小往大来，吉亨", "泰，小往大来", "天地交泰，君子以辅相天地之宜", "土", "通泰之卦，和谐顺畅", "地天", "长男", "通泰、和谐", "春", "中央"),
            createHexagram(12L, "否卦", "000111", "否之匪人，不利君子贞", "否，泰之反", "天地不交，君子以俭德辟世", "土", "闭塞之卦，困顿不通", "天地", "长男", "否塞、困顿", "夏", "中央"),
            createHexagram(13L, "同人卦", "101111", "同人于野，亨", "同人，柔得位得中而应乎乾", "天火同人，君子以类族辨物", "火", "同人之卦，团结协作", "天火", "长男", "同人、团结", "春夏之交", "南方"),
            createHexagram(14L, "大有卦", "111101", "元亨", "大有，柔得尊位大中而上下应之", "火在天上，君子以遏恶扬善顺天休命", "火", "大有之卦，丰盛富足", "火天", "长男", "大有、丰盛", "春夏之交", "南方"),
            createHexagram(15L, "谦卦", "001110", "亨，君子有终", "谦，亨，天道下济而光明", "地中有山，君子以裒多益寡称物平施", "土", "谦虚之卦，谦逊有礼", "地山", "长男", "谦虚、礼让", "秋", "东北方"),
            createHexagram(16L, "豫卦", "011110", "利建侯行师", "豫，刚应而志行", "雷出地奋，君子以作乐崇德", "木", "愉悦之卦，安逸快乐", "雷地", "长男", "愉悦、安逸", "春", "东方"),
            createHexagram(17L, "随卦", "101101", "元亨利贞，无咎", "随，刚来而下柔", "泽中有雷，君子以向晦入宴息", "金", "随从之卦，顺应变化", "泽雷", "长男", "随从、顺应", "秋", "西方"),
            createHexagram(18L, "蛊卦", "100110", "元亨，利涉大川", "蛊，刚上而柔下", "山下有风，君子以振民育德", "土", "整治之卦，拨乱反正", "山风", "长男", "整治、革新", "夏", "东北方"),
            createHexagram(19L, "临卦", "110000", "元亨利贞，至于八月有凶", "临，刚浸而长", "地泽临，君子以教思无穷容保民无疆", "土", "临下之卦，亲临指导", "地泽", "长男", "亲临、指导", "冬", "西南方"),
            createHexagram(20L, "观卦", "000110", "盥而不荐，有孚颙若", "观，盥而不荐", "风行地上，先王以省方观民设教", "木", "观察之卦，审时度势", "风地", "长男", "观察、审慎", "春", "东南方"),
            createHexagram(21L, "噬嗑卦", "100101", "亨，利用狱", "噬嗑，亨，利用狱", "雷电噬嗑，先王以明罚敕法", "火", "刑罚之卦，明辨是非", "火雷", "长男", "刑罚、明断", "夏", "东方"),
            createHexagram(22L, "贲卦", "100111", "亨，小利有攸往", "贲，亨，柔来而文刚", "山下有火，君子以明庶政无敢折狱", "火", "文饰之卦，美化修饰", "山火", "长男", "文饰、美化", "夏", "东北方"),
            createHexagram(23L, "剥卦", "000001", "不利有攸往", "剥，剥也，柔变刚也", "山附于地，君子以厚下安宅", "土", "剥落之卦，衰落消沉", "山地", "长男", "剥落、衰落", "秋", "东北方"),
            createHexagram(24L, "复卦", "100000", "亨，出入无疾", "复，亨，刚反动而以顺行", "雷在地中，先王以至日闭关", "木", "回复之卦，重新开始", "地雷", "长男", "回复、复兴", "冬", "东方"),
            createHexagram(25L, "无妄卦", "101110", "元亨利贞，其匪正有眚", "无妄，刚中而应", "天下有雷，君子以茂对时育万物", "木", "无妄之卦，真实无伪", "天雷", "长男", "无妄、真实", "冬", "西北方"),
            createHexagram(26L, "大畜卦", "101100", "利贞，不家食吉", "大畜，刚健笃实辉光", "天在山中，君子以多识前言往行以畜其德", "土", "大畜之卦，积蓄力量", "山天", "长男", "大畜、积蓄", "春夏之交", "东北方"),
            createHexagram(27L, "颐卦", "100001", "贞吉，观颐，自求口实", "颐，贞吉，养正则吉", "山下有雷，君子以慎言语节饮食", "土", "颐养之卦，修养身心", "山雷", "长男", "颐养、修养", "春夏之交", "东北方"),
            createHexagram(28L, "大过卦", "011111", "栋桡，利有攸往，亨", "大过，大者过也", "泽灭木，君子以独立不惧遁世无闷", "金", "过度之卦，非常之举", "泽风", "长男", "大过、过度", "夏", "西方"),
            createHexagram(29L, "坎卦", "010010", "习坎，有孚维心亨", "坎，习坎，重险也", "水洊至，君子以常德行习教事", "水", "险陷之卦，困难重重", "坎水", "中男", "险陷、困难", "冬", "北方"),
            createHexagram(30L, "离卦", "101101", "利贞，亨，畜牝牛吉", "离，丽也", "明两作离，大人以继明照于四方", "火", "附丽之卦，光明美丽", "离火", "中女", "附丽、光明", "夏", "南方"),
            createHexagram(31L, "咸卦", "011100", "亨利贞，取女吉", "咸，感也", "山上有泽，君子以虚受人", "金", "感应之卦，相互吸引", "泽山", "少女", "感应、吸引", "春夏之交", "西方"),
            createHexagram(32L, "恒卦", "100110", "亨，无咎，利贞", "恒，久也", "雷风恒，君子以立不易方", "木", "恒久之卦，持久不变", "雷风", "长男", "恒久、持久", "春夏之交", "东方"),
            createHexagram(33L, "遁卦", "111100", "亨，小利贞", "遁，亨，遁而亨也", "天下有山，君子以远小人不恶而严", "金", "退避之卦，适时隐退", "天山", "长男", "遁退、隐退", "夏", "西北方"),
            createHexagram(34L, "大壮卦", "001111", "利贞", "大壮，大者壮也", "雷在天上，君子以非礼弗履", "木", "强盛之卦，壮大发展", "雷天", "长男", "大壮、强盛", "春夏之交", "东方"),
            createHexagram(35L, "晋卦", "101000", "康侯用锡马蕃庶", "晋，进也", "明出地上，君子以自昭明德", "火", "晋升之卦，向上发展", "火地", "长男", "晋升、发展", "春夏之交", "南方"),
            createHexagram(36L, "明夷卦", "000101", "利艰贞", "明夷，明入地中", "明入地中，君子以莅众用晦而明", "土", "明夷之卦，韬光养晦", "地火", "长男", "明夷、韬晦", "秋冬之交", "南方"),
            createHexagram(37L, "家人卦", "101011", "利女贞", "家人，女正位乎内", "风自火出，君子以言有物行有恒", "木", "家庭之卦，家庭和睦", "风火", "长男", "家人、家庭", "春夏之交", "东南方"),
            createHexagram(38L, "睽卦", "110101", "小事吉", "睽，火动而上泽动而下", "上火下泽，君子以同而异", "火", "睽违之卦，意见不合", "火泽", "长男", "睽违、分歧", "夏", "南方"),
            createHexagram(39L, "蹇卦", "010100", "利西南，不利东北", "蹇，难也", "山上有水，君子以反身修德", "水", "艰难之卦，困难重重", "水山", "长男", "蹇难、困难", "冬", "东北方"),
            createHexagram(40L, "解卦", "001010", "利西南，无所往其来复吉", "解，险以动", "雷雨作解，君子以赦过宥罪", "木", "解脱之卦，化解困难", "雷水", "长男", "解脱、化解", "春", "东方"),
            createHexagram(41L, "损卦", "100100", "有孚元吉，无咎", "损，损下益上", "山下有泽，君子以惩忿窒欲", "土", "损益之卦，减损增益", "山泽", "长男", "损益、调节", "秋冬之交", "东北方"),
            createHexagram(42L, "益卦", "001001", "利有攸往，利涉大川", "益，损上益下", "风雷益，君子以见善则迁有过则改", "木", "益助之卦，增益发展", "风雷", "长男", "增益、发展", "春夏之交", "东南方"),
            createHexagram(43L, "夬卦", "111110", "扬于王庭，孚号有厉", "夬，决也", "泽上于天，君子以施禄及下居德则忌", "金", "决断之卦，果断决策", "泽天", "长男", "决断、果断", "春夏之交", "西方"),
            createHexagram(44L, "姤卦", "011111", "女壮，勿用取女", "姤，柔遇刚也", "天下有风，后以施命诰四方", "金", "相遇之卦，不期而遇", "天风", "长男", "相遇、邂逅", "夏", "西南方"),
            createHexagram(45L, "萃卦", "011000", "亨，王假有庙", "萃，聚也", "泽上于地，君子以除戎器戒不虞", "土", "聚集之卦，汇聚力量", "泽地", "长男", "萃聚、汇聚", "夏", "西方"),
            createHexagram(46L, "升卦", "001101", "元亨，用见大人", "升，柔以时升", "地中生木，君子以顺德，积小以高大", "木", "升进之卦，稳步上升", "地风", "长男", "升进、成长", "春", "东南方"),
            createHexagram(47L, "困卦", "010011", "亨，贞大人吉", "困，刚掩也", "泽无水，君子以致命遂志", "金", "困穷之卦，身处困境", "泽水", "长男", "困穷、困境", "秋冬之交", "西方"),
            createHexagram(48L, "井卦", "110100", "改邑不改井", "井，德之地也", "木上有水，君子以劳民劝相", "水", "井养之卦，滋养万物", "水风", "长男", "井养、滋养", "冬", "北方"),
            createHexagram(49L, "革卦", "101010", "巳日乃孚，元亨利贞", "革，水火相息", "泽中有火，君子以治历明时", "金", "变革之卦，改革创新", "泽火", "长男", "变革、革新", "秋冬之交", "西方"),
            createHexagram(50L, "鼎卦", "010101", "元吉亨", "鼎，象也", "木上有火，君子以正位凝命", "火", "鼎新之卦，建立新秩序", "火风", "长男", "鼎新、建立", "春夏之交", "南方"),
            createHexagram(51L, "震卦", "001000", "亨，震来虩虩", "震，亨，震来虩虩", "洊雷震，君子以恐惧修省", "木", "震动之卦，震动惊惧", "震雷", "长男", "震动、惊惧", "春", "东方"),
            createHexagram(52L, "艮卦", "000100", "艮其背，不获其身", "艮，止也", "兼山艮，君子以思不出其位", "土", "静止之卦，停止静止", "艮山", "少男", "静止、停止", "冬", "东北方"),
            createHexagram(53L, "渐卦", "101001", "女归吉，利贞", "渐，女归吉也", "山上有木，君子以居贤德善俗", "木", "渐进之卦，循序渐进", "风山", "长女", "渐进、循序", "春", "东南方"),
            createHexagram(54L, "归妹卦", "100101", "征凶，无攸利", "归妹，天地之大义也", "泽上有雷，君子以永终知敝", "金", "归妹之卦，婚姻嫁娶", "雷泽", "少女", "归妹、婚姻", "春", "西方"),
            createHexagram(55L, "丰卦", "101111", "亨，王假之", "丰，大也", "雷电皆至，君子以折狱致刑", "火", "丰盛之卦，盛大繁荣", "雷火", "长男", "丰盛、繁荣", "夏", "东方"),
            createHexagram(56L, "旅卦", "011010", "小亨，旅贞吉", "旅，小亨", "山上有火，君子以明慎用刑而不留狱", "火", "旅行之卦，旅途奔波", "火山", "长男", "旅行、奔波", "夏", "东北方"),
            createHexagram(57L, "巽卦", "110110", "小亨，利有攸往", "巽，小亨，柔顺乎刚中而志行", "随风巽，君子以申命行事", "木", "巽入之卦，顺从进入", "巽风", "长女", "巽入、顺从", "夏", "东南方"),
            createHexagram(58L, "兑卦", "110011", "亨，利贞", "兑，说也", "丽泽兑，君子以朋友讲习", "金", "喜悦之卦，愉悦喜悦", "兑泽", "少女", "喜悦、愉悦", "秋", "西方"),
            createHexagram(59L, "涣卦", "010110", "亨，王假有庙", "涣，风行水上", "风行水上，君子以散释前眚", "水", "涣散之卦，解散消散", "风水", "长男", "涣散、消散", "春", "东南方"),
            createHexagram(60L, "节卦", "010010", "亨，苦节不可贞", "节，亨，刚柔分而刚得中", "泽上有水，君子以制数度议德行", "水", "节制之卦，适度节制", "水泽", "长男", "节制、适度", "秋冬之交", "西方"),
            createHexagram(61L, "中孚卦", "110011", "豚鱼吉，利涉大川", "中孚，柔在内而刚得中", "泽上有风，君子以议狱缓死", "木", "诚信之卦，诚信待人", "风泽", "长女", "中孚、诚信", "春夏之交", "东南方"),
            createHexagram(62L, "小过卦", "001011", "亨，利贞", "小过，小者过而亨也", "山上有雷，君子以行过乎恭丧过乎哀用过乎俭", "木", "小过之卦，小有过度", "雷山", "少男", "小过、过度", "夏", "东北方"),
            createHexagram(63L, "既济卦", "101010", "亨小，利贞", "既济，亨小者，阴阳之正也", "水在火上，君子以思患而豫防之", "水", "既济之卦，事情完成", "水火", "长男", "既济、完成", "夏", "北方"),
            createHexagram(64L, "未济卦", "010101", "亨，小狐汔济", "未济，男之穷也", "火在水上，君子以慎辨物居方", "火", "未济之卦，事情未完成", "火水", "长男", "未济、未完成", "夏", "南方")
        );
        
        // 保存卦象数据
        hexagramRepository.saveAll(hexagrams);
        
        // 为每个卦象创建爻辞数据（简化示例，实际应该包含详细的爻辞内容）
        for (Hexagram hexagram : hexagrams) {
            createYaoCiForHexagram(hexagram.getId());
        }
    }
    
    private Hexagram createHexagram(Long id, String name, String symbol, String hexagramMeaning, 
                                  String tuanMeaning, String xiangMeaning, String fiveElements, 
                                  String description, String naturalSymbol, String familyRelationship, 
                                  String personalityTraits, String season, String direction) {
        Hexagram hexagram = new Hexagram();
        hexagram.setId(id);
        hexagram.setName(name);
        hexagram.setSymbol(symbol);
        hexagram.setHexagramMeaning(hexagramMeaning);
        hexagram.setTuanMeaning(tuanMeaning);
        hexagram.setXiangMeaning(xiangMeaning);
        hexagram.setFiveElements(fiveElements);
        hexagram.setDescription(description);
        hexagram.setNaturalSymbol(naturalSymbol);
        hexagram.setFamilyRelationship(familyRelationship);
        hexagram.setPersonalityTraits(personalityTraits);
        hexagram.setSeason(season);
        hexagram.setDirection(direction);
        return hexagram;
    }
    
    private void createYaoCiForHexagram(Long hexagramId) {
        // 为每个卦象创建6个爻辞（简化示例）
        for (int i = 1; i <= 6; i++) {
            YaoCi yaoCi = new YaoCi();
            yaoCi.setHexagramId(hexagramId);
            yaoCi.setYaoPosition(i);
            yaoCi.setYaoNature(i % 2 == 0 ? "阴" : "阳");
            yaoCi.setYaoCiText("第" + i + "爻爻辞内容示例");
            yaoCi.setYaoSymbol("爻象内容示例");
            yaoCi.setChangingYaoCi("变爻辞内容示例");
            yaoCiRepository.save(yaoCi);
        }
    }
}